public class DeleteHCPEducationAllInfo
{
     public String UrlId
    {
        get{        
            return System.currentPageReference().getParameters().get('id');     
        }  
        set;
    }

    public PageReference  del()
    {
        try{
        if(UrlId == null){
            pageReference pr = new pageReference('/a0W/o');    
            pr.setredirect(true);      
            return pr;
        }
        HCPEducation__c HCPEducation = [Select id,date__c,ownerid From HCPEducation__c where id =: UrlId limit 1];
        Profile GMp  = [select id from profile where name=: 'System Administrator' or name =: '系统管理员']; 
        if((HCPEducation.date__c <= date.today())&&userinfo.getprofileid() != GMp.id){
            delete HCPEducation;
            pageReference pr = new pageReference('/a0W/o');    
            pr.setredirect(true);      
            return pr;
        }
        
        List<RegionNameList__c>Rnl = [Select id,name From RegionNameList__c Where HCPEducation__c =: UrlId];
        List<CityNameList__c> Cnl;
        List<NationalAttendee__c> NA;
        List<JoinPeople__c>JP;
        if(Rnl.size() > 0){
            if(Rnl.size() > 0)
                Cnl = [Select id,name From CityNameList__c Where RegionNameList__c in: Rnl];
            
            NA = [Select id,name From NationalAttendee__c Where HCPEducation__c =: UrlId];
            if(NA.size() > 0)
                delete NA;
                            
            if(Cnl.size() > 0){
                JP = [Select id,name From JoinPeople__c Where CityNameList__c in: Cnl];
                if(JP.size() > 0)
                    delete JP;
             }
             if(Cnl != null){
                 if(Cnl.size() > 0)
                     delete Cnl;
             }
                
            if(Rnl.size() > 0)
                delete Rnl;
            delete HCPEducation;
        }
        else{
            List<JoinPeople__c>JPSaloon = [Select id,name From JoinPeople__c Where NationalHCPEducation__c =: UrlId];
            List<NationalAttendee__c>NAPS = [Select id,name From NationalAttendee__c Where HCPEducation__c =: UrlId];
            if(JPSaloon.size() > 0){
                delete JPSaloon;
            }
            if(NAPS.size() > 0){
                delete NAPS;
            }
            delete HCPEducation;               
        } 
            pageReference pr = new pageReference('/a0W/o');    
            pr.setredirect(true);      
            return pr;
        }
        catch (DmlException e) {
            ApexPages.addMessages(e);
            return null;     
         } 
    }
    
    static testMethod void testcase()
    {
    
        HCPEducation__c nhcpe = new HCPEducation__c();
        nhcpe.Name = 'a';
        nhcpe.date__c = date.Today().addDays(3);
        nhcpe.place__c = 'a';
        insert nhcpe;

        DeleteHCPEducationAllInfo testCase = new DeleteHCPEducationAllInfo(); 
        testCase.UrlId = nhcpe.id;
        System.currentPageReference().getParameters().put('id', nhcpe.Id); 
        testCase.del();
    }    

}